<?php

require_once ('Zend/Controller/Action.php');
require_once('Zend/Filter/Input.php');
require_once('Zend/Json.php');

abstract class Gpc_Controller_BaseStatsModel extends Zend_Controller_Action {

   protected $modelName = null;
   protected $returnController = null;
   protected $returnAction = null;
   
   /**
    * For processing and filtering data.
    */
   abstract protected function _filter();
   

   /**
    * Filters, validates and inserts data into `stats` table.
    */
   public function addAction() {
      /* Processes input data through filters and validation */
      $data = $this->_filter();
      
      Zend_Loader::loadClass($this->modelName);
      $model = new $this->modelName();
      
      try {
         $id = $model->insert($data);
      } catch ( Exception $e ) {
         throw $e;
      }
      
      // TODO: Need to get ID and return it, too
      $data['success'] = true;
      $data['id'] = $id;
      
      $return = $data;
      $this->_processReturn($return);
   }
   
   /**
    * Filters, validates and inserts data into `stats` table.
    */
   public function updateAction() {
      Zend_Loader::loadClass($this->modelName);
      $model = new $this->modelName();
      
      $id = (int)$this->getRequest()->getParam('id');
      
      /* Processes input data through filters and validation */
      $data = $this->_filter();
      
      $where = $model->getAdapter()->quoteInto('id = ?', $id);
      $model->update($where, $data);
      
      $return = array('success' => true, 'id' => $id);
      $this->_processReturn($return);
   }
   
   /**
    * Deletes a specific row from `stats`
    * 
    * @param $id  Row ID of stat info to return.
    */
   public function deleteAction() {
      
      $return = array();
      /* Process the delete ONLY if it's an AJAX request or a confirmed standard response */
      if ( $this->getRequest()->isXmlHttpRequest() || 
           $this->getRequest()->getParam('confirm') == 'Okay' ) {

         $signature = $this->getRequest()->getParam('signature');
         
         if ( empty($signature) || $signature !== Zend_Registry::get('session')->signature ) {
            throw new Exception("Unable to delete record: unable to validate request", -666);
         }
         
         $id = (int)$this->getRequest()->getParam('id');
         
         if ( $id > 0 ) {
            Zend_Loader::loadClass($this->modelName);
            $model = new $this->modelName();
            $rowset = $model->find($id);
            $row = $rowset->current();
            $row->delete();
            
            $return = array('success' => true, 'id' => $id);
         } else {
            throw new Exception("No ID Supplied", -123);
         }
      }
      
      $this->_processReturn($return);
   }
   
   /**
    * Processes the return. If AJAX request, send JSON data, else forward to 
    *    toons admin page.
    */
   protected function _processReturn( $return ) {
            
      /* If it's not an AJAX Request, redirect back to toons page */
      if ( ! $this->getRequest()->isXmlHttpRequest() ) {
         $this->_redirect('/admin/'.$this->returnController . '/' . $this->returnAction);
         exit;
      }
      
      /* AJAX Request, return JSON */
      $json = Zend_Json::encode($return);
      
      /* Prevent the View and Layout from being redered */
      $this->_helper->layout()->disableLayout();  
      $this->_helper->viewRenderer->setNoRender();
      
      $this->getResponse()->setHeader('X-JSON', $json); // JSON header
      print $json; // JSON body for browsers like Safari which break the JSON header.
      exit;
   }
}
